﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Management;
using System.Globalization;



namespace SmartRS.com.vn.smartrs.controller
{
    public class PrinterController
    {
        #region Properties and Variables
        PrintDocument pdoc = null;
        private decimal _price = 0;

        public PrinterController()
        {
        }
        #endregion



        #region Print Page using Printer
        public void print()
        {
            try
            {

                PrintDialog pd = new PrintDialog();
                string strDefaultPrinter = pd.PrinterSettings.PrinterName;//Code to get default printer name  
                pdoc = new PrintDocument();
                PrinterSettings ps = new PrinterSettings();
                Font font = new Font("Courier New", 15);//set default font for page
                PaperSize psize = new PaperSize("Custom", 212, 130);//set paper size sing code
                pd.Document = pdoc;
                pd.Document.DefaultPageSettings.PaperSize = psize;
                pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);

                //*************************Code to set Default Printer*************************************

                string defaultPrinterName = ps.PrinterName; // Code to get default printer

                if (defaultPrinterName == "GC420GT")
                {
                    ps.PrinterName = "GC420GT";//Code to set default printer name
                    pd.PrinterSettings.PrinterName = "GC420GT";//Code to set default printer name 
                }
                else
                {
                    ps.PrinterName = defaultPrinterName;//Code to set default printer name
                    pd.PrinterSettings.PrinterName = defaultPrinterName;//Code to set default printer name 
                }


                //************************************End**************************************************


                DialogResult result = pd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    pdoc.Print();

                }


                //***************************************************************End*****************************************************************//

            }
            catch (Exception ex)
            {


            }

        }

        void pdoc_PrintPage(object sender, PrintPageEventArgs e)
        {

            StringFormat sf = new StringFormat();
            //-------------------------------------End-----------------------------------------------------//


            Font font = new Font("Courier New", 10);// set default font size
            float fontHeight = font.GetHeight();
            int startX = 5;// Position of x-axis
            int startY = 2;//starting position of y-axis
            int Offset = 0;
            e.Graphics.DrawString("xxxxxxxx", new Font("Courier New", 9), new SolidBrush(System.Drawing.Color.Black), startX, startY + Offset);
            Offset = Offset + 16;
            e.Graphics.DrawString("$" + "100", new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(System.Drawing.Color.Black), startX, startY + Offset);
            Offset = Offset + 20;
            //********************************************End********************************************

            Offset = 0;
        }
        #endregion





    }
}